package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.operator.Equal;
import com.ejlchina.searcher.util.MapUtils;
import com.sky.context.BaseContext;
import com.sky.dto.AddressBookDto;
import com.sky.entity.AddressBook;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import com.sky.utils.BeanCopyUtil;
import com.sky.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper,AddressBook> implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private BeanSearcher beanSearcher;

    /**
     * 查询用户的所有地址信息
     * @param
     * @return
     */
    public List<AddressBook> selectAddressBook() {
        Long userId = BaseContext.getCurrentUserId();
        Map<String, Object> build =
                MapUtils.builder()
                        .field(AddressBook::getUserId,userId).op(Equal.class)
                        .build();
        return beanSearcher.searchAll(AddressBook.class,build);
    }



    /**
     * 根据id查询
     *
     * @param id 用户地址id
     * @return
     */
    public AddressBook getById(Long id) {
        Long userId = BaseContext.getCurrentUserId();
        Map<String, Object> build =
                MapUtils.builder()
                        .field(AddressBook::getUserId,userId).op(Equal.class)
                        .field(AddressBook::getId,id).op(Equal.class)
                        .build();
        return beanSearcher.searchFirst(AddressBook.class,build);
    }

    /**
     * 查询默认地址
     */
    @Override
    public AddressBook selectAddressBookByUserIdDefault() {
        Long userId = BaseContext.getCurrentUserId();
        Map<String, Object> build =
                MapUtils.builder()
                        .field(AddressBook::getUserId,userId).op(Equal.class)
                        .field(AddressBook::getIsDefault,1).op(Equal.class)
                        .build();
        return beanSearcher.searchFirst(AddressBook.class,build);
    }

    /**
     * 新增用户地址信息
     *
     * @param dto 用户地址信息
     */
    public int insertAddressBook(AddressBookDto dto) {
        AddressBook addressBook = new AddressBook();
        BeanCopyUtil.copyProperties(dto,addressBook);
        addressBook.setUserId(BaseContext.getCurrentUserId());
        addressBook.setIsDefault(0);
        addressBook.setCreateBy(BaseContext.getCurrentUserName());
        addressBook.setCreateTime(DateUtils.getNowDate());
        addressBook.setUpdateBy(BaseContext.getCurrentUserName());
        addressBook.setUpdateTime(DateUtils.getNowDate());
        return addressBookMapper.insert(addressBook);
    }

    /**
     * 根据id修改地址
     *
     * @param dto 用户地址信息
     */
    public int updateAddressBook(AddressBookDto dto) {
        AddressBook addressBook = new AddressBook();
        BeanCopyUtil.copyProperties(dto,addressBook);
        addressBook.setUpdateBy(BaseContext.getCurrentUserName());
        addressBook.setUpdateTime(DateUtils.getNowDate());
        return addressBookMapper.updateById(addressBook);
    }

    /**
     * 设置默认地址
     *
     * @param dto 地址信息
     */
    @Transactional
    public int setDefault(AddressBookDto dto) {
        //1、将当前用户的所有地址修改为非默认地址
        Long userId = BaseContext.getCurrentUserId();
        UpdateWrapper<AddressBook> wrapper = new UpdateWrapper<>();
        wrapper.in("user_id",userId);
        wrapper.set("is_default","0");
        wrapper.set("update_time", DateUtils.getNowDate());
        wrapper.set("update_by", BaseContext.getCurrentUserName());
        addressBookMapper.update(null,wrapper);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        AddressBook addressBook = new AddressBook();
        BeanCopyUtil.copyProperties(dto,addressBook);
        addressBook.setIsDefault(1);
        addressBook.setUpdateBy(BaseContext.getCurrentUserName());
        addressBook.setUpdateTime(DateUtils.getNowDate());
        return addressBookMapper.updateById(addressBook);
    }

    /**
     * 根据id删除地址
     *
     * @param id 地址id
     */
    public int deleteById(Long id) {
        Long userId = BaseContext.getCurrentUserId();
        UpdateWrapper<AddressBook> wrapper = new UpdateWrapper<>();
        wrapper.in("id",id);
        wrapper.set("del_flag","2");
        wrapper.set("update_time", DateUtils.getNowDate());
        wrapper.set("update_by", BaseContext.getCurrentUserName());
        return addressBookMapper.update(null,wrapper);
    }
}
